//---------------------------------------------------------------------------------------//
package 
{

	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import it.gotoandplay.smartfoxserver.*;
	import it.gotoandplay.smartfoxserver.data.User;
	
	
	//----------------------------------------------------------------------------------------//
	
	//--------------------------------------------------------------------------------------//
	public class MekanetClient extends Sprite
	{
		
		public var sfc:SmartFoxClient;
		private var testObject:Object = {};
		private var grid:Grid;
			
		private var userName:String;
		
			
		//players
		public var players:Array;
		private var mainPlayerMoved:Boolean = true;
		
		
		
		//-------------------------------------------------------------------------------//
		public function MekanetClient()
		{
			userName = new String();
			
			grid = new Grid();
			players = new Array();
						
				
			this.addChild(grid);
			
			
			this.addEventListener(Event.ADDED_TO_STAGE,Added);
			
			
		}
		public function Added(ev:Event):void
		{
			
			SimpleKeyboard.Init(this.stage);
		}
		//-------------------------------------------------------------------------------//
		public function Init():void
		{
			
			sfc = new SmartFoxClient(false);
			sfc.addEventListener(SFSEvent.onConnection,OnConnection);
			sfc.addEventListener(SFSEvent.onExtensionResponse,OnExtensionResponse);
			sfc.addEventListener(SFSEvent.onLogin,OnLogin);
			sfc.addEventListener(SFSEvent.onJoinRoom,OnJoinRoom);
			sfc.addEventListener(SFSEvent.onRoomLeft,OnRoomLeft);
			sfc.addEventListener(SFSEvent.onJoinRoomError,OnJoinRoomError);
			sfc.addEventListener(SFSEvent.onRoomListUpdate,onRoomListUpdate);
			
			sfc.addEventListener(SFSEvent.onUserEnterRoom,OnUserEnterRoom);
			sfc.addEventListener(SFSEvent.onUserLeaveRoom,OnUserLeaveRoom);
			
			sfc.addEventListener(SFSEvent.onObjectReceived,OnObjectRecieved);			
			
			
			grid.addEventListener(MouseEvent.MOUSE_DOWN,MouseDownOnSquare);
			
			
			//Me conecto al smartfox
			
			//sfc.connect("danielbrenner.com.ar");
			//sfc.connect("190.177.194.84");
			sfc.connect("192.168.200.238");
			
			
			
			
			
			
		}
		//-------------------------------------------------------------------------------//
		public function OnConnection(ev:SFSEvent):void
		{
			if (ev.params.success)
			{
				trace ("Me conecte a IP "+sfc.ipAddress+
				" Puerto:"+sfc.port);
				
				//sfc.login("sampleZone","","");
				sfc.login("simpleChat",userName,"");
				
				
			}
			else 
			{
				trace("Error in the connection");
			}
		}
		//-------------------------------------------------------------------------------//
		public function OnLogin(sfc:SFSEvent):void
		{
			
			if (sfc.params.success)
			{
				userName = sfc.params.name;
				var newPlayer:Player = new Player();
				newPlayer.name = userName;
				newPlayer.Init();
				players.push(newPlayer);
				this.addChild(newPlayer);
				
				
				trace ("Welcome "+userName);
			}
			else
			{
				trace ("Error logging in: "+sfc.params.error);
			}
			
			
		}
		//-------------------------------------------------------------------------------//
		public function OnJoinRoom(ev:SFSEvent):void
		{

			trace ("room: "+ ev.params.room + " joined");
			
			//testObject.values = [100,200,300,400,500];
			//sfc.sendXtMessage("myExt","double",testObject,"xml");
		
			var obj:Object = new Object();
			obj.enterRoom = true;
			obj.name = sfc.myUserName;
			obj.color = players[0].color;
			obj.x = players[0].x;
			sfc.sendObject(obj);
			trace (obj.name+" "+obj.x);
			
			this.addEventListener(Event.ENTER_FRAME,Update);
		}
		//-------------------------------------------------------------------------------//
		public function OnRoomLeft(ev:SFSEvent):void
		{
			
			
		}
		//-------------------------------------------------------------------------------//
		public function OnJoinRoomError(ev:Event):void
		{
			trace ("error joining room");
		}
		//-------------------------------------------------------------------------------//
		public function OnUserEnterRoom(ev:SFSEvent):void
		{
			
			
			var obj:Object = new Object();
			obj.enterRoom = true;
			obj.color = players[0].color;
			obj.x = players[0].x;
			sfc.sendObject(obj);
			/*
			var usArr:Array = new Array();
			usArr.push(user);
			
			sfc.sendObjectToGroup(obj,usArr);
			*/
		}
		//-------------------------------------------------------------------------------//
		public function OnUserLeaveRoom(ev:SFSEvent):void
		{
			
				
			for each (var player:Player in players)
			{
				if (player.name ==ev.params.userName)
				{
					this.removeChild(player);
					players.splice(players.indexOf(player),1);
					break;		
				}					
			}
		}
		//-------------------------------------------------------------------------------//
		public function onRoomListUpdate(ev:Event):void
		{
			trace ("room list updated");
			sfc.autoJoin();
		}
		//-------------------------------------------------------------------------------//
		public function OnExtensionResponse(obj:Object):void
		{
			trace ("me respondio");
			
			var dataObj:Object = obj.params.dataObj;
			var cmd:String = dataObj._cmd;
			
			//aca manejo la respuesta
			var val:Object;
			if (cmd == "double")
			{
				for each (val in dataObj.values)
					trace(val);
				
				sfc.sendXtMessage("myExt","sayHola",testObject);
			}
			if (cmd == "sayHola")
			{
				trace (dataObj.values);
			}
		}
		//-------------------------------------------------------------------------------//
		public function MouseDownOnSquare(ev:MouseEvent):void
		{
			
			var index:int = -1;
			var shap:Shape = null;
			for each (shap in grid.squares)
			{
				if (shap.hitTestPoint(ev.localX,ev.localY))
				{
					shap.visible = false;
					index = grid.squares.indexOf(shap);
					break;
				}
			}
			if (shap != null)
			{
				var obj:Object=new Object();
				obj.index = index;
				obj.visible = shap.visible;
				sfc.sendObject(obj);
			}
		}
		//-------------------------------------------------------------------------------//
		public function KeyDown(ev:KeyboardEvent):void
		{
			
			
			
		}
		//-------------------------------------------------------------------------------//
		public function OnObjectRecieved(ev:SFSEvent):void
		{
			//var obj:Object = ev.params.obj;
			var user:User = ev.params.sender;
			if (ev.params.obj.enterRoom)
			{
				var playerToAdd:Player;
				trace("broadcast de que un usuario entro al cuarto");
				var playerInArray:Boolean = false;
				for each (var player:Player in players)
				{
					if (player.name == user.getName())
					{
						playerInArray = true;
					}
				}
				if (playerInArray == false)
				{
					playerToAdd = new Player();
					playerToAdd.color = ev.params.obj.color;
					playerToAdd.name = user.getName();
					playerToAdd.x = ev.params.obj.x;
					playerToAdd.Init();
					players.push(playerToAdd);
					this.addChild(playerToAdd);
				}
			}
			else if (ev.params.obj.update)
			{
				for each (var p:Player in players)
				{
					if (p.name == user.getName())
					{
						p.x = ev.params.obj.x;
						
					}
				}
			}
			else
			{
				trace (ev.params.obj.px);
				var squareClient:Shape = grid.squares[ev.params.obj.index];
				if (squareClient)
					squareClient.visible = ev.params.obj.visible ;
			}
		}
		//-------------------------------------------------------------------------------//
		public function Update(ev:Event):void
		{
			//players[0].Update();
			var xx:Keyboard;
			if (SimpleKeyboard.KeyDown(Keyboard.LEFT))
			{
				players[0].x-=1;
				mainPlayerMoved = true;
			}
			else if (SimpleKeyboard.KeyDown(Keyboard.RIGHT))
			{
				players[0].x+=1;
				mainPlayerMoved = true;
			}
			else
			{
				mainPlayerMoved = false;
			}
			
			
			
			if (mainPlayerMoved)
			{
				var obj:Object;
				obj = new Object();
				obj.update = true;
				obj.x = players[0].x;
				sfc.sendObject(obj);
				mainPlayerMoved = false;
			}
			
				
				
		}
		//-------------------------------------------------------------------------------//
	}
	//-----------------------------------------------------------------------------------//
}
//---------------------------------------------------------------------------------------//
